package daily;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CallableAndFuture {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService threadPool = Executors.newFixedThreadPool(10);

		CompletionService<String> completionService = new ExecutorCompletionService<String>(threadPool);

		for (int i = 0; i < 10; i++) {
			completionService.submit(new Callable<String>() {

				@Override
				public String call() throws Exception {
					Thread.sleep(1000);
					return Thread.currentThread().getName();
				}

			});
		}

//		Future<String> future = threadPool.submit(new Callable<String>() {
//
//			@Override
//			public String call() throws Exception {
//				Thread.sleep(2000);
//				return "Halo";
//			}
//
//		});

//		System.out.println(future.get());
		
		for (int i = 0; i < 10; i++) {
			System.out.println(completionService.take().get());
		}

		threadPool.shutdown();
	}

}
